<?php

  $con = mysql_connect("host","root","root");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("aygrid", $con);

mysql_query("SET NAMES 'UTF8'");

$pro = '';

  if (isset($_GET['pro']))$pro = $_GET['pro'];

function acatch($pro){

  switch($pro){

  case 'getStu': 

  

  $beginN = 0;

  $countP = 1000;

  if (isset($_POST['beginN']))$beginN = $_POST['beginN'] - 1;

  if (isset($_POST['countP']))$countP = $_POST['countP'];

  

  $result = mysql_query("SELECT `id` FROM `ayGrid_stu`");

  $count = mysql_num_rows($result); 

  

  $result = mysql_query("SELECT `id`,`stuName`,`department`,`creditHour`,`passed`,`mydate` FROM `ayGrid_stu` LIMIT ". $beginN * $countP ." , ".$countP." ");

  $encodable = array();

  $mencodable = array();

  $mencodable['count'] = $count;

  

  while($row = mysql_fetch_assoc($result))

  {

  $encodable[] = $row;

  }

  $mencodable['data'] = $encodable;

  $encoded = json_encode($mencodable);

  return $encoded;

  case 'psd':

  $id = 0;

  $stuName = '';

  $department = 0;

  $creditHour = 0;

  $psd = 0;

  if (isset($_POST['id']))$id = $_POST['id'];

  if (isset($_POST['stuName']))$stuName = $_POST['stuName'];

  if (isset($_POST['department']))$department = $_POST['department'];

  if (isset($_POST['creditHour']))$creditHour = $_POST['creditHour'];

  if (isset($_POST['psd']))$psd = $_POST['psd'];

  

  $result = mysql_query("update ayGrid_stu a set stuName = '$stuName',department = $department, creditHour = $creditHour, passed = $psd where a.id = $id");

  return $result;

  case 'add':

  $data = 0;

  if (isset($_POST['data']))$data = $_POST['data'];

  $sql = 'insert into ayGrid_stu (stuName,department,creditHour,passed)values';

  

  $vStr = '';

  foreach ($data as $rows) {

  $vStr = $vStr . "('" . $rows['stuName']. "'," .$rows['department']. ',' .$rows['department']. ',' .$rows['department'] . '),';

  }

  $vStr = substr($vStr,0,strlen($vStr)-1);

  $sql = $sql . $vStr;

  $result = mysql_query($sql);

  return $result;

  case 'dpt':

  $result = mysql_query("SELECT `id`,`pName` FROM `ayGrid_dep`");

  $encodable = array();

  

  while($row = mysql_fetch_assoc($result))

  {

  $encodable[] = $row;

  

  }

  $encoded = json_encode($encodable);

  return $encoded;

  }

  }

  header("Content-type: application/json;charset=utf-8");

  echo acatch($pro); 

  mysql_close($con);

  ?>